<?php
if(defined('IZ_ACCNAME')) {
  $opt_flags = $_SESSION['IZ_OPT'];
  if($_SESSION['canview'] == 1 || $_GET['act'] == 'login') {
    if(is_file($_SESSION['IZ_CALPATH'].'includes/themes/'.$_SESSION['IZ_CURTHEME'].'.php')) {
      include_once($_SESSION['IZ_CALPATH'].'includes/themes/'.$_SESSION['IZ_CURTHEME'].'.php');
    }
    $colwidth = 'narrow';
    $includeadd = true;
    if(isset($_REQUEST['events']) || isset($_REQUEST['todo'])) {
      $maininclude = $_SESSION['IZ_CALPATH'].'includes/manage.php';
    }
    if(empty($_GET) && empty($_POST) || isset($_POST['setusercust']) || $_GET['v']) {
      $daynum = $curDay;
      $monthnum = $curMonth;
      $yearnum = $curYear;
      $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
      $view = $_SESSION['IZ_DEFAULTVIEW'];
      $mainpage = true;
    } elseif(!empty($_GET['view'])) {
      switch($_GET['view']) {
        case 'day':
          if(empty($_GET['day'])) { $daynum = date('j',mktime()); } else { $daynum = $_GET['day']; }
          if(empty($_GET['month'])) { $monthnum = date('n',mktime()); } else { $monthnum = $_GET['month']; }
          if(empty($_GET['year'])) { $yearnum = date('Y',mktime()); } else { $yearnum = $_GET['year']; }
          $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
          $view = 'day';
          $ptitle = ' &#187; '.iz_longdate($secs);
          $mainpage = true;
          break;
        case 'week':
          if(empty($_GET['day'])) { $daynum = date('j',mktime()); } else { $daynum = $_GET['day']; }
          if(empty($_GET['month'])) { $monthnum = date('n',mktime()); } else { $monthnum = $_GET['month']; }
          if(empty($_GET['year'])) { $yearnum = date('Y',mktime()); } else { $yearnum = $_GET['year']; }
          $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
          $view = 'week';
          $firstdaynum = $daynum - getOffsetDisplayedIndex(date('w',$secs));
          $ptitle = ' &#187; '.STR_WEEKSTARTING.' '.iz_longdate(mktime(0,0,0,$monthnum,$firstdaynum,$yearnum));
          $mainpage = true;
          break;
        case 'month':
          $daynum = 1;
          if(empty($_GET['month'])) { $monthnum = date('n',mktime()); } else { $monthnum = $_GET['month']; }
          if(empty($_GET['year'])) { $yearnum = date('Y',mktime()); } else { $yearnum = $_GET['year']; }
          $secs = mktime(0,0,0,$monthnum,1,$yearnum);
          $monthend = mktime(0,0,0,$monthnum+1,0,$yearnum);
          $view = 'month';
          $ptitle = ' &#187; '.$monthnames[date('n',$secs)].' '.$yearnum;
          $mainpage = true;
          break;
        case 'extmonth':
          $daynum = 1;
          if(empty($_GET['month'])) { $monthnum = date('n',mktime()); } else { $monthnum = $_GET['month']; }
          if(empty($_GET['year'])) { $yearnum = date('Y',mktime()); } else { $yearnum = $_GET['year']; }
          $secs = mktime(0,0,0,$monthnum,1,$yearnum);
          $monthend = mktime(0,0,0,$monthnum+1,0,$yearnum);
          $view = 'extmonth';
          $ptitle = ' &#187; '.$monthnames[date('n',$secs)].' '.$yearnum;
          $colwidth = 'wide';
          $mainpage = true;
          break;
        case 'year':
          $daynum = 1;
          $monthnum = 1;
          if(empty($_GET['day'])) { $daynum = date('j',mktime()); } else { $daynum = $_GET['day']; }
          if(empty($_GET['year'])) { $yearnum = date('Y',mktime()); } else { $yearnum = $_GET['year']; }
          $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
          $view = 'year';
          $ptitle = ' &#187; '.date('Y',$secs);
          $colwidth = 'wide';
          $mainpage = true;
          break;
        case 'list':
          $ptitle = ' &#187; '.STR_VIEWEVENTS;
          $viewlist = true;
          $daynum = $curDay;
          $monthnum = $curMonth;
          $yearnum = $curYear;
          $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
          $view = $_SESSION['IZ_DEFAULTVIEW'];
          $sortabletable = true;
          $jshead .= '<script type="text/javascript" src="'.$_SESSION['IZ_CALURL'].'includes/js/tablesort.js"></script>';
          $colwidth = 'wide';
          break;
        case 'expanded':
          $ptitle = ' &#187; '.STR_VIEWEVENTS;
          $viewexpanded = true;
          $daynum = $curDay;
          $monthnum = $curMonth;
          $yearnum = $curYear;
          $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
          $view = $_SESSION['IZ_DEFAULTVIEW'];
          $colwidth = 'wide';
          break;
      }
    } elseif(!empty($_GET['act'])) {
      //$_POST *should* be empty
      switch($_GET['act']) {
        case 'manage':
          $ptitle = ' &#187; '.STR_VIEWEVENTS;
          $sortabletable = true;
          $jshead .= '<script type="text/javascript" src="'.$_SESSION['IZ_CALURL'].'includes/js/tablesort.js"></script>';
          $colwidth = 'wide';
          break;
        case 'viewevent':
          $ptitle = ' &#187; '.STR_VIEWINGEVENT;
          $colwidth = 'wide';
          break;
        case 'viewtodo':
          $ptitle = ' &#187; '.STR_VIEWINGTASK;
          $colwidth = 'wide';
          break;
        case 'search':
          $ptitle = ' &#187; '.STR_SEARCH;
          $sortabletable = true;
          $jshead .= '<script type="text/javascript" src="'.$_SESSION['IZ_CALURL'].'includes/js/tablesort.js"></script>';
          $colwidth = 'wide';
          $maininclude = $_SESSION['IZ_CALPATH'].'includes/search.php';
          break;
        case 'register':
          $ptitle = ' &#187; '.STR_REGISTER;
          $maininclude = $_SESSION['IZ_CALPATH'].'includes/register.php';
          $jshead .= '<script type="text/javascript" src="'.$_SESSION['IZ_CALURL'].'includes/js/colorpick.js"></script>
                      <link rel="stylesheet" type="text/css" href="'.$_SESSION['IZ_CALURL'].'includes/colorpick.css" />
                      <script type="text/javascript" src="'.$_SESSION['IZ_CALURL'].'includes/lib/scriptaculous.js?load=builder,slider,dragdrop"></script>
                      <script src="'.$_SESSION['IZ_CALURL'].'includes/lib/yahoo.color.js" type="text/javascript"></script>
                      ';
          break;
        case 'report':
          $ptitle = ' &#187; '.STR_REPORTS;
          $maininclude = $_SESSION['IZ_CALPATH'].'includes/reports.php';
          $colwidth = 'wide';
          $jshead .= '<script type="text/javascript" src="'.$_SESSION['IZ_CALURL'].'includes/lib/fusion/FusionCharts.js"></script>'."\n";
          header("Pragma: public"); // IE caches the chart
          header("Expires: 0");
          header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
          header("Cache-Control: private",false);
          break;
        case 'add':
          $ptitle = ' &#187; '.STR_ADD;
          $maininclude = $_SESSION['IZ_CALPATH'].'includes/add.php';
          $includeadd = false;
          break;
       case 'login':
         $ptitle = ' &#187; '.STR_LOGIN;
         $maininclude = $_SESSION['IZ_CALPATH'].'includes/login.php';
         $onload .= 'document.getElementById(\'username\').focus();'."\n";
         $login = true;
         break;
        case 'logout':
          $daynum = $curDay;
          $monthnum = $curMonth;
          $yearnum = $curYear;
          $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
          $view = $_SESSION['IZ_DEFAULTVIEW'];
          $mainpage = true;
          break;
      }
    } elseif(!empty($_POST['act'])) {
      // $_GET is empty
      switch($_POST['act']) {
        case 'search':
          $ptitle = ' &#187; '.STR_SEARCH;
          $sortabletable = true;
          $jshead .= '<script type="text/javascript" src="'.$_SESSION['IZ_CALURL'].'includes/js/tablesort.js"></script>';
          $colwidth = 'wide';
          $maininclude = $_SESSION['IZ_CALPATH'].'includes/search.php';
          break;
        case 'editevent':
          $ptitle = ' &#187; '.STR_EDITEVENT;
          $colwidth = 'wide';
          $editevent = true;
          break;
        case 'edittodo':
          $ptitle = ' &#187; '.STR_EDITTASK;
          $colwidth = 'wide';
          break;
       case 'login':
         if($error) {
           $ptitle = ' &#187; '.STR_LOGIN;
           $maininclude = $_SESSION['IZ_CALPATH'].'includes/login.php';
           $onload .= 'document.getElementById(\'username\').focus();'."\n";
           $login = true;
         }
         break;
      }
    } elseif($_POST['mass'] == 'edit') {
      $ptitle = ' &#187; '.STR_EDITINGEVENTS;
      $colwidth = 'wide';
      $massedit = true;
    } elseif(!empty($_GET['category'])) {
      if(array_key_exists($_GET['category'],$_SESSION['eventcats'])) {
        $ptitle = ' &#187; '.STR_VIEWINGCATEGORY.'&#58; '.$_SESSION['eventcats'][$_GET['category']]->name;
      } elseif(array_key_exists($_GET['category'],$_SESSION['todocats'])) {
        $ptitle = ' &#187; '.STR_VIEWINGCATEGORY.'&#58; '.$_SESSION['todocats'][$_GET['category']]->name;
      } else {
        $ptitle = ' &#187; '.STR_VIEWINGCATEGORY;
      }
      $sortabletable = true;
      $jshead .= '<script type="text/javascript" src="'.$_SESSION['IZ_CALURL'].'includes/js/tablesort.js"></script>';
      $pagecategory = true;
      $colwidth = 'wide';
    }
  ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  
  <head profile="http://gmpg.org/xfn/11">
  
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="generator" content="iZeit <?php echo IZ_CALVERSION; ?>" />
  <title><?php echo $_SESSION['IZ_CALNAME'],$ptitle ?></title>
  <link rel="shortcut icon" href="http://favicon.izeit.nu/favicon.ico" type="image/vnd.microsoft.icon" />
  <link rel="icon" href="http://favicon.izeit.nu/favicon.ico" type="image/vnd.microsoft.icon" />
  <link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['IZ_CALURL']; ?>includes/main.css" />
  <link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['IZ_CALURL']; ?>includes/datepicker.css" />
  <link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['IZ_CALURL']; ?>includes/themes/<?php echo $_SESSION['IZ_CURTHEME']; ?>.css" />
  <link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['IZ_CALURL']; ?>includes/data/usercss.css" />
  <?php getcss(); ?>
  <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/lib/prototype.js"></script>
  <?php if(function_exists('gzdeflate')) { ?>
    <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/lib/tiny_mce/tiny_mce_gzip.js"></script>
    <script type="text/javascript">
    tinyMCE_GZ.init({
    	plugins : 'inlinepopups,emotions',
    	themes : 'advanced',
    	languages : 'en',
    	disk_cache : true,
    	debug : false
    });
    </script>
  <?php } else { ?>
    <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/lib/tiny_mce/tiny_mce.js"></script>
  <?php } ?>
  <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/lib/scriptaculous.js?load=effects,controls"></script>
  <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/js/datepicker.js"></script>
  <?php 
  if(file_exists($_SESSION['IZ_CALPATH'].'includes/lang/'.$_SESSION['IZ_LANG'].'.js')) {
    echo '<script type="text/javascript" src="',$_SESSION['IZ_CALURL'],'includes/lang/',$_SESSION['IZ_LANG'],'.js"></script>'; 
  } else {
    echo '<script type="text/javascript" src="',$_SESSION['IZ_CALURL'],'includes/lang/en.js"></script>
  '; 
  }
  echo $jshead;
  ?>
  <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/js/ajax.js"></script>
  <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/js/main.js"></script>
  <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/js/onload.js"></script>
  <script type="text/javascript" src="<?php echo $_SESSION['IZ_CALURL']; ?>includes/js/validate.js"></script>
  <script type="text/javascript">
  //<![CDATA[
  JS_DFORMAT = '%d/%m/%Y';
  var izimgdir = '<?php echo $_SESSION['IZ_CALURL']; ?>images/';
  var JS_FIRSTDAY = <?php echo $_SESSION['IZ_FIRSTDAY']; ?>;
  <?php if($pageregister) {
    echo 'var JS_USERBLANK = \'',ERR_USERBLANK,'\'',"\n",
         'var JS_CONFIRMPASSFAIL = \'',ERR_CONFIRMPASSFAIL,'\'',"\n",
         'var JS_PASSBLANK = \'',ERR_PASSBLANK,'\'',"\n";
  } ?>
  url = '<?php echo $_SESSION['IZ_CALURL']; ?>index.php';
  fromloc = '<?php echo str_replace(array("=","&"),array("_","#"),$_SERVER['QUERY_STRING']); ?>';
  calurl = '<?php echo $_SESSION['IZ_CALURL']; ?>';
  <?php get_categories('jslist'); ?>
  //]]>
  </script>
  </head>
  
  <body<?php 
          if($sortabletable) { 
            echo ' onload="tableruler();"';
          } elseif($editevent) {
            echo ' onload="setallday(\'main\',\'edit\');"'; 
          } 
       ?>>
  <div <?php echo 'id="',$colwidth,'"'; ?>>
  <div id="customisewrapper"><div class="tabs" id="customisepanel">&nbsp;</div></div>
    <div id="bannerback"><?php
      if($_SESSION['IZ_REPLACELOGO']) {
        echo '<h1><a href="',$_SESSION['IZ_CALURL'],'">',$_SESSION['IZ_CALNAME'],'</a></h1>';
      } else {
        echo '<a href="',$_SESSION['IZ_CALURL'],'" id="banner">iZeit Home</a>';
      }
  ?></div>
    <div id="column">
  
    <div id="navmenu">
      <div id="navleft">
        <?php getnavleft(); ?>
      </div>
      <div id="navright">
        <?php getnavright(); ?>
      </div>
    </div>
  
    <?php if($includeadd) {
      include($_SESSION['IZ_CALPATH'].'includes/add.php'); 
    } ?>
  
    <div id="calwrapper">
    <?php 
    if(!$login) {
      if($_SESSION['authed'] == 1) {
        echo '<p id="loginlink">',STR_LOGGEDIN,' <strong>',$_SESSION['uname'],'</strong> &#40;<a href="',$_SESSION['IZ_CALURL'],'index.php?act=logout">',STR_LOGOUT,'</a>&#41;</p>'; 
      } else {
        echo '<p id="loginlink">',STR_NOTLOGGEDIN,' &#40;<a href="',$_SESSION['IZ_CALURL'],'index.php?act=login">',STR_LOGIN,'</a>';
        if(($_SESSION['IZ_OPT'] & OPT_REG) != 0) {
          echo ' &#124; <a href="',$_SESSION['IZ_CALURL'],'index.php?act=register">',STR_REGISTER,'</a>';
        }
        echo '&#41;</p>'; 
      } 
    } ?>
  
    <?php if(!empty($error) && !$login) { echo '<div id="errorbox2">',$error,'</div>'; } ?>
  
    <br />
    <div id="calbody">
    <?php
    if($maininclude) {
      include($maininclude);
    } elseif($pagecategory) {
      showcategory($_GET['category']);
    } elseif($mainpage) {
      if(($opt_flags & OPT_TODO) != 0) {
    	  echo '<div id="todolist">';
  	    gettodolist(0);
    	  echo '</div><!-- end todolist -->';
      }
      if(($opt_flags & OPT_GOTO) != 0) {
        getnavmenu($daynum,$monthnum,$yearnum);
      }
      echo '<br />';
      if(($opt_flags & OPT_FILTERS) != 0) { 
        cat_filter('main'); 
      }
      echo '<br /><div id="ajaxdiv">';
      switch($view) {
        case 'year':
          echo get_year($secs);
          break;
        case 'extmonth':
          echo get_month($secs,'nav','nolink','ext');
          break;
        case 'month':
          echo get_month($secs,'nav','nolink');
          break;
        case 'week':
          echo get_week($secs);
          break;
        case 'day':
          echo get_day($secs);
          break;
      }
      echo '</div>';
    }
    if($massedit) {
      massedit();
    }
    ?>
  </div> <!-- end calbody -->
  <br />
  <?php //} ?>
  </div> <!-- end calwrapper -->
  <?php //} ?>
  <br />
  </div><!-- end column -->
  </div><!-- end narrow or wide -->
  <?php echo '<p class="gentext"><a href="&#x68;&#x74;&#x74;&#112;&#58;&#x2F;&#47;&#x77;&#x77;&#x77;&#46;&#105;&#x7A;&#x65;&#105;&#x74;&#x2E;&#110;&#x75;">iZeit ',IZ_CALVERSION,'</a><br />Copyright <a href="&#x68;&#x74;&#x74;&#112;&#58;&#x2F;&#47;&#x6C;&#x65;&#x67;&#104;&#117;&#x6D;&#x70;&#101;&#x64;&#x2E;&#99;&#x6F;&#109;" style="text-decoration: none;">&copy;</a>2005 - ',date("Y"),' <a href="http://&#000119;&#000119;&#x77;&#46;&#x6c;&#x69;&#x6d;&#000101;&#00052;&#57;&#46;&#x63;&#x6f;&#x6d;">&#72;&#x61;&#x72;&#114;&#121;&#x20;&#74;&#x65;&#x6E;&#x6E;&#101;&#114;&#x77;&#x61;&#121;</a></p>'; ?>
  <?php if($sortabletable) {  get_cdata(); } ?>
  <script type="text/javascript">
  //<![CDATA[
  <?php echo $cals; ?>
  listen('load', window, function() {
  <?php echo $onload; ?>
  });/*End onload*/
  //]]>
  </script>
  <?php if(!empty($jsfoot)) {
  echo '  <script type="text/javascript">
    ',$jsfoot,'
  </script>'; } ?>
  </body>
  </html>
  <?php 
  } else {
    // Can't view
    header("Location: ".$_SESSION['IZ_CALURL']."index.php?act=login&from=act-default");
  }
} else {
  // Directly accessed
  header('Location: ../');
} ?>